﻿(function($){
$.fn.dataOption=function(options){
	var me=$(this);
	
	var oname="";//select optgroup
	if(me.is('select')||me.is('optgroup')){
	}else{
		if(options["objectName"]){
			oname=options["objectName"];
		}else if(me.attr("id")){
			oname=me.attr("id").substring(0,me.attr("id").length-1);
		}else{
			alert("object without [name] attribute");
			return me;
		}
	}
	
	var setting=$.extend({
		dataurl:null
		,async:true
		,datasource:null
		,init:[]
		,dataOptionId:"id"
		,delimiter:""
		,dataOptionValue:"value"
		,optionType:"checkbox"
		,objectName:oname
		,className:""
		,labelWithValue:""
		,callback:$.noop
		,buttonSet:true
		,markLabel:false
		,options:{start:0,genDatasource:null,doPad:false}
	}
	,options||{});
	if(setting.markLabel){
		markLabel(me);
	}
	setting.options.start=setting.options.start|0;
	if(setting.options.genDatasource){
		if($.isArray(setting.options.genDatasource)){
			var os=setting.options.genDatasource;
			$.each(os,function(i){
				os[i]={"id":os[i],"value":os[i]};
			});
			setting.datasource=os;
		}else{
			var os=new Array();
			os.length=setting.options.genDatasource;
			
			var padlen=0;
			if(setting.options.doPad){
				padlen=(new String(os.length)).length;
			}
			
			$.each(os,function(i){
				var str=i+setting.options.start;
				
				for(var k=(new String(str)).length;k<padlen;k++){
					str="0"+str;
				}
				
				os[i]={"id":str,"value":str};
			});
		}
		setting.datasource=os;
		
	}
	
	if(setting.datasource){
		populate(setting.datasource);
	}else{
		$.ajax({
			url:setting.dataurl
			,async:setting.async
				,type:"POST"
				,dataType:"json"
				,success:function(json){populate(json);}
		});		
	}
	
	function populate(json){
		for(var i=0;i<json.length;i++){
			var name=setting.objectName;
			var val=json[i][setting.dataOptionId];
			var lbl=json[i][setting.dataOptionValue];
			var lblClassName=json[i].className;
			var id=name+"_"+val;
			if(setting.labelWithValue){
				if(setting.labelWithValue=="("){
					lbl=lbl+"("+val+")";
				}else{
					lbl=lbl+setting.labelWithValue+val;
				}
			}
			//if("option"==setting.optionType){

			if(me.is('select')||me.is('optgroup')){
				var checked="";
				$.each(setting.init,function(i,e){
					if(e==val){
						checked=" selected='selected' ";
					}
				});
				me.append(
					$("<option "+checked+">")
					.attr("id",id)
					.val(val)
					.text(lbl)
					.addClass(setting.className)
				);
			}else{
				if(i>0){
					me.append(setting.delimiter);
				}
				var div=$("<div>").css({"white-space":"nowrap","display":"inline"});
				var checked="";
				
				$.each(setting.init,function(i,e){
					if(e==val){
						checked=" checked='checked' ";
					}
				});
				 
				var oopt=$("<input name='"+name+"' "+checked+" type='"+setting.optionType+"' id='"+id+"'  value='"+val+"'>");
				

				div.append(oopt);
				var olbl=$("<label>");
				olbl.attr("for",id);
				olbl.text(lbl);
				olbl.addClass(lblClassName);
				olbl.addClass(setting.className);
				
				div.append(olbl);
				me.append(div);
				
			}
		}
		
		if(me.is('select')){
		}else{
			if(setting.buttonSet){
				me.buttonset();				
			}
		}
		setting.callback();		
	}
	function markLabel(o){
		if($(o).is(":radio")){
			var labels=$("[for^="+$(o).attr("name")+"]");
			$.each(labels.toArray(),function(i,label){
				$(label).removeClass("ui-state-active");
			});
		}
		var label=$("[for="+$(o).attr("id")+"]");
		if($(o).is(":checked")){		
			label.addClass("ui-state-active");
		}else{
			label.removeClass("ui-state-active");
		}
	}	

	return $(this);
};
})(jQuery);